﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GeneralServices
{
    /// <summary>
    /// Logique d'interaction pour BtnIcon.xaml
    /// </summary>
    public partial class BtnIcon : UserControl
    {
        #region Command 

        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(BtnIcon), new PropertyMetadata((ICommand)null, new PropertyChangedCallback(CommandChanged)));

        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(BtnIcon), new PropertyMetadata(null));

        public static readonly RoutedEvent MouseClickEvent = EventManager.RegisterRoutedEvent(
        "MouseClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(BtnIcon));

        public object CommandParameter
        {
            get
            {
                return GetValue(CommandParameterProperty);
            }
            set
            {
                SetValue(CommandParameterProperty, value);
            }
        }

        public IInputElement CommandTarget
        {
            get;
            set;
        }

        public ICommand Command
        {
            get
            {
                return (ICommand)GetValue(CommandProperty);
            }
            set
            {
                SetValue(CommandProperty, value);
            }
        }

        private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BtnIcon b = (BtnIcon)d;
            b.HookUpCommand((ICommand)e.OldValue, (ICommand)e.NewValue);
        }

        private void HookUpCommand(ICommand oldCommand, ICommand newCommand)
        {
            if (oldCommand != null)
            {
                RemoveCommand(oldCommand);
            }

            AddCommand(newCommand);
        }

        private void RemoveCommand(ICommand oldCommand)
        {
            EventHandler handler = CanExecuteChanged;

            oldCommand.CanExecuteChanged -= handler;
        }

        private void AddCommand(ICommand newCommand)
        {
            EventHandler handler = new EventHandler(CanExecuteChanged);

            if (newCommand != null)
                newCommand.CanExecuteChanged += handler;
        }

        private void CanExecuteChanged(object sender, EventArgs e)
        {
            if (this.Command != null)
            {
                RoutedCommand command = this.Command as RoutedCommand;

                if (command != null)
                {
                    if (command.CanExecute(CommandParameter, CommandTarget))
                    {
                        this.IsEnabled = true;
                    }
                    else
                    {
                        this.IsEnabled = false;
                    }
                }
                //If not routed command
                else
                {
                    if (Command.CanExecute(CommandParameter))
                    {
                        this.IsEnabled = true;
                    }
                    else
                    {
                        this.IsEnabled = false;
                    }
                }
            }
        }

        public event RoutedEventHandler MouseClick
        {
            add
            {
                AddHandler(MouseClickEvent, value);
            }
            remove
            {
                RemoveHandler(MouseClickEvent, value);
            }
        }

        protected void OnMouseClick()
        {
            RoutedEventArgs args = new RoutedEventArgs(BtnIcon.MouseClickEvent);
            RaiseEvent(args);
        }

        #endregion

        #region Color

        public static readonly DependencyProperty TextColorProperty =
            DependencyProperty.Register(
                "TextColor", typeof(Brush),
                typeof(BtnIcon),
                new PropertyMetadata(new SolidColorBrush(Color.FromRgb(0,0,0)))
            );

        public static readonly new DependencyProperty BackgroundProperty =
            DependencyProperty.Register(
                "Background", typeof(Brush),
                typeof(BtnIcon),
                new PropertyMetadata(null)
           );

        public new Brush Background
        {
            get
            {
                return (Brush)GetValue(BackgroundProperty);
            }
            set
            {
                SetValue(BackgroundProperty, value);
            }
        }


        public Brush TextColor
        {
            get
            {
                return (Brush)GetValue(TextColorProperty);
            }
            set
            {
                SetValue(TextColorProperty, value);
            }
        }

        #endregion

        #region Property

        public ImageSource SourceImg
        {
            get { return (ImageSource)GetValue(SourceImgProperty); }
            set { SetValue(SourceImgProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SourceImg.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SourceImgProperty =
            DependencyProperty.Register("SourceImg", typeof(ImageSource), typeof(BtnIcon), new PropertyMetadata(null));


        public String Title
        {
            get { return (String)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(String), typeof(BtnIcon), new PropertyMetadata(String.Empty));

        #endregion

        public BtnIcon()
        {
            InitializeComponent();
            btn.Click += btn_Click;
            //TextColor = new SolidColorBrush(Color.FromArgb(100,2,168,223));
        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            if (Command != null)
            {
                RoutedCommand command = Command as RoutedCommand;

                if (command != null)
                {
                    command.Execute(CommandParameter, CommandTarget);
                }
                else
                {
                    Command.Execute(CommandParameter);
                }
            }
        }
    }
}
